//
//  UIButton+ZCLExtension.m
//  blue
//
//  Created by YiXue on 16/10/14.
//  Copyright © 2016年 YiXue. All rights reserved.
//

#import "UIButton+ZCLExtension.h"
#import <objc/runtime.h>

static char YXEnlargeTopNameKey;
static char YXEnlargeRightNameKey;
static char YXEnlargeBottomNameKey;
static char YXEnlargeLeftNameKey;

@implementation UIButton (ZCLExtension)



- (void)setYXEnlargeEdgeWithTop:(CGFloat)top right:(CGFloat)right bottom:(CGFloat)bottom left:(CGFloat)left {
    objc_setAssociatedObject(self, &YXEnlargeTopNameKey, [NSNumber numberWithFloat:top], OBJC_ASSOCIATION_COPY_NONATOMIC);
    objc_setAssociatedObject(self, &YXEnlargeRightNameKey, [NSNumber numberWithFloat:right], OBJC_ASSOCIATION_COPY_NONATOMIC);
    objc_setAssociatedObject(self, &YXEnlargeBottomNameKey, [NSNumber numberWithFloat:bottom], OBJC_ASSOCIATION_COPY_NONATOMIC);
    objc_setAssociatedObject(self, &YXEnlargeLeftNameKey, [NSNumber numberWithFloat:left], OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (CGRect)getYXEnlargedRect {
    NSNumber* topEdge = objc_getAssociatedObject(self, &YXEnlargeTopNameKey);
    NSNumber* rightEdge = objc_getAssociatedObject(self, &YXEnlargeRightNameKey);
    NSNumber* bottomEdge = objc_getAssociatedObject(self, &YXEnlargeBottomNameKey);
    NSNumber* leftEdge = objc_getAssociatedObject(self, &YXEnlargeLeftNameKey);
    if (topEdge && rightEdge && bottomEdge && leftEdge) {
        return CGRectMake(self.bounds.origin.x - leftEdge.floatValue,
                          self.bounds.origin.y - topEdge.floatValue,
                          self.bounds.size.width + leftEdge.floatValue + rightEdge.floatValue,
                          self.bounds.size.height + topEdge.floatValue + bottomEdge.floatValue);
    } else {
        return self.bounds;
    }
}

-(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
    CGRect rect=[self getYXEnlargedRect];
    if(CGRectEqualToRect(rect, self.bounds))
    {
        return [super pointInside:point withEvent:event];
    }
    return CGRectContainsPoint(rect, point)?YES:NO;

}

/**
 * 不可变Btn
 */

+(UIButton *)leftimageAndRighttitlePatternWithImage:(NSString *)imageName AndMargin:(CGFloat)margin AndTitle:(NSString *)title AndTitleFont:(UIFont *)titlefont AndTitleColor:(UIColor *)titleColor
{
    
    UIButton *btn = [[UIButton alloc]init];
    //样式
    btn.titleLabel.font = titlefont;
    [btn setTitleColor:titleColor forState:UIControlStateNormal];
    
    //内容
    UIImage *normalimage = [UIImage imageNamed:imageName];
    [btn setImage:normalimage forState:UIControlStateNormal];
    [btn setTitle:title forState:UIControlStateNormal];
    
    
    //调整frame
    CGFloat imageWith = normalimage.size.width;
    CGFloat labelWidth = [self sizeWithString:title font:titlefont].width;
    
    btn.bounds = CGRectMake(0, 0,imageWith+labelWidth+8, normalimage.size.height+2);
    btn.titleEdgeInsets = UIEdgeInsetsMake(0, margin, 0, -margin);
    
    return btn;

}
+ (CGSize)sizeWithString:(NSString *)string font:(UIFont *)font{
    CGRect rect = [string boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX)//限制最大高度
                                       options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesFontLeading |NSStringDrawingUsesLineFragmentOrigin//采用换行模式
                                    attributes:@{NSFontAttributeName: font}//传人的字体字典
                                       context:nil];
    return rect.size;
}
- (void)changeTitle:(NSString *)title AndMarign:(CGFloat)margin
{
    
    [self setTitle:title forState:UIControlStateNormal];
    UIImage *normalimage = self.currentImage;
    UIFont *titlefont = self.titleLabel.font;
    
    CGFloat imageWith = normalimage.size.width;
    CGFloat labelWidth = [self sizeWithString:title font:titlefont].width;
    
    self.bounds = CGRectMake(0, 0,imageWith+labelWidth+8, normalimage.size.height+2);
    self.titleEdgeInsets = UIEdgeInsetsMake(0, margin, 0, -margin);
    
}
- (CGSize)sizeWithString:(NSString *)string font:(UIFont *)font{
    CGRect rect = [string boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX)//限制最大高度
                                       options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesFontLeading |NSStringDrawingUsesLineFragmentOrigin//采用换行模式
                                    attributes:@{NSFontAttributeName: font}//传人的字体字典
                                       context:nil];
    return rect.size;
}


@end
